package com.CST.controller;

import com.CST.common.api.Response;
import com.CST.common.api.ResultObject;

import com.CST.entity.BdOrganization;
import com.CST.entity.BdOrganizationRule;
import com.CST.service.IBdOrganizationService;
import com.CST.util.PageHelperTool;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>科室部门对应</p>
 *
 * @author: y
 * @create: 2021-10-22 14:31
 **/
@Api(tags = {"科室部门对应"})
@RestController
@RequestMapping("/BdOrganization")
@Log4j2
public class BdOrganizationController {

    @Autowired
    private IBdOrganizationService targetService;

    @PostMapping("/getOrganizationSource")
    @ApiOperation("获取科室部门对应数据,必传参数:ruleCode:规则edExtraFirstCode = 1的edCode, yard :1")
    public ResultObject getOrganizationSource(@RequestBody BdOrganization entity) {
        if (entity.getRuleCode().isEmpty() || entity.getYard() == 0) {
            return ResultObject.errorReturn("对应规则编号ruleCode/所属院区yard不能为空");
        }
        List<BdOrganization> result = targetService.getOrganizationSource(entity);
        if (CollectionUtils.isEmpty(result)) {
            return ResultObject.successReturn(new ArrayList<BdOrganization>());
        }
        result.forEach(item -> {
            if (item.getPDeptCode() == null || item.getPDeptCode().isEmpty()) {
                item.setRelationStatus("0");
            } else {
                item.setRelationStatus("1");
            }
        });
        if (!entity.getRelationStatus().isEmpty()) {
            result = result.stream().filter(t -> t.getRelationStatus().equals(entity.getRelationStatus())).collect(Collectors.toList());
        }
        return ResultObject.successReturn(PageHelperTool.excutePageInfo(result, entity.getPageIndex(), entity.getPageSize()));
    }

    @PostMapping("/getOrganizationNormal")
    @ApiOperation("获取科室部门对应数据,必传参数:ruleCode:规则edExtraFirstCode = 1的edCode, yard :1")
    public ResultObject getOrganizationNormal(@RequestBody BdOrganization entity) {
        if (entity.getRuleCode().isEmpty() || entity.getYard() == 0) {
            return ResultObject.errorReturn("对应规则编号ruleCode/所属院区yard不能为空");
        }
        List<BdOrganization> result = targetService.getOrganizationNormal(entity);
        if (CollectionUtils.isEmpty(result)) {
            return ResultObject.successReturn(new ArrayList<BdOrganization>());
        }
        return ResultObject.successReturn(result);
    }

    @PostMapping("/getNoRelationDepartmentList")
    @ApiOperation("获取科室部门对应数据,必传参数:ruleCode:规则edExtraFirstCode = 1的edCode, yard :1")
    public ResultObject getNoRelationDepartmentList(@RequestBody BdOrganization entity) {
        if (entity.getRuleCode().isEmpty() || entity.getYard() == 0) {
            return ResultObject.errorReturn("对应规则编号ruleCode/所属院区yard不能为空");
        }
        List<BdOrganization> result = targetService.getNoRelationDepartmentList(entity);
        return ResultObject.successReturn(result);
    }

    @PostMapping("/addOrganization")
    @ApiOperation("添加科室部门对应数据,必传参数:ruleCode:规则edExtraFirstCode = 1的edCode,yard :1")
    public ResultObject addOrganization(@RequestBody BdOrganization entity) {
        if (entity.getRuleCode().isEmpty() || entity.getYard() == 0) {
            return ResultObject.errorReturn("对应规则编号ruleCode/所属院区yard不能为空");
        }
        Response result = targetService.addOrganization(entity);
        return ResultObject.resultReturn(result);
    }

    @PostMapping("/modifyOrganization")
    @ApiOperation("修改科室部门对应数据,必传参数:ruleCode:规则edExtraFirstCode = 1的edCode,yard :1")
    public ResultObject modifyOrganization(@RequestBody BdOrganization entity) {
        if (entity.getRuleCode().isEmpty() || entity.getYard() == 0) {
            return ResultObject.errorReturn("对应规则编号ruleCode/所属院区yard不能为空");
        }
        Response result = targetService.modifyOrganization(entity);
        return ResultObject.resultReturn(result);
    }

    @PostMapping("/batchDeleteOrganization")
    @ApiOperation("删除对应关系, 传id 多个以逗号隔开")
    public ResultObject batchDeleteOrganization(@RequestBody List<Integer> ids) {
        if (ids == null || ids.stream().count() == 0) {
            return ResultObject.errorReturn("请选择要删除的记录");
        }
        Response result = targetService.batchDeleteOrganization(ids);
        return ResultObject.resultReturn(result);
    }

    @PostMapping("/addInheritanceRules")
    @ApiOperation("添加 规则继承关系，deptCode将作为parentRuleCode传入")
    public Response addInheritanceRules(@RequestBody BdOrganizationRule entity) {
        return targetService.addInheritanceRules(entity);
    }

    @GetMapping("/deleteRule")
    @ApiOperation("删除规则")
    public Response deleteRule(Integer edId, String ruleCode) {
        boolean result = targetService.deleteRule(edId, ruleCode);
        if (!result) {
            return Response.error("删除失败");
        }
        return Response.success("删除成功");
    }

    @PostMapping("/listParentDeptGroup")
    @ApiOperation("获取规则表")
    public ResultObject listParentDeptGroup(String yard) {
        return targetService.listParentDeptGroup(yard);
    }

    @PostMapping("/listClinicalPDeptGroup")
    @ApiOperation("获取所有临床类父级科室")
    public ResultObject listClinicalPDeptGroup() {
        List<BdOrganization> list = targetService.listClinicalPDeptGroup();
        return ResultObject.successReturn(list);
    }


    @PostMapping("/downloadTemplate")
    @ApiOperation("科室部门对应导出下载")
    public void downloadTemplate(HttpServletResponse response, @RequestBody BdOrganization dto) throws Exception {
        targetService.exportExcel(response, dto);
    }

}
